package com.woniu.inlet.web.controller;

import com.woniu.service.bean.MessageReceivedCommand;
import com.woniu.service.bean.SendMessageCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "这是测试接口")
public class MessageController {

    /**
     * 未来，消息真正意义上的生产者，调用可靠消息服务的这个接口，要求可靠消息服务【帮】它发一条消息。
     * 该消息是发给参数 exchange，路由键是参数 routingKey，消息的具体内容是参数 messageContent
     */
    @ApiOperation("这是发送消息方法") //required = true 必填
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exchange",value = "交换机",required = true),
            @ApiImplicitParam(name = "routing_key",value = "路由键",required = true),
            @ApiImplicitParam(name = "message_content",value = "消息内容",required = true)
    })
    @PostMapping("/message/send")
    public boolean sendMessage(

            @RequestParam("exchange") String exchange,
            @RequestParam("routing_key") String routingKey,
            @RequestParam("message_content") String messageContent) {

        SendMessageCommand command = new SendMessageCommand(exchange, routingKey, messageContent);
        command.execute();
        return true;
    }

    /**
     * 未来，消息的消费者，调用可靠消息服务的这个接口，
     * 借此【告知】可靠消息服务参数 messageId 所代表的消息已被消费。
     */
    @ApiOperation("这是接收者接收消息方法") //required = true 必填
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "消息id",required = true)
    })
    @PostMapping("/message/{id}/received")
    public boolean receivedMessage(@PathVariable("id") Integer messageId) {
        MessageReceivedCommand command = new MessageReceivedCommand(messageId);
        command.execute();
        return true;
    }

}
